﻿using HHG.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnLineExam.Web
{
	public partial class view : System.Web.UI.Page
	{ 
		private readonly Data.EfRepository<Data.Score> _scoreRepository;
        private readonly Data.EfRepository<Data.Paper> _paperRepository;
		private readonly Data.EfRepository<Data.UserAnswer> _userAnswerRepository;
		Data.OnlineExamEntities _ctx;

        public string PaperName { get;private set; }

		

		public view() {
            _ctx = new Data.OnlineExamEntities();
            _scoreRepository = new Data.EfRepository<Data.Score>(_ctx);
            _paperRepository = new Data.EfRepository<Data.Paper>(_ctx);
			_userAnswerRepository = new Data.EfRepository<Data.UserAnswer>(_ctx);


		}
		protected void Page_Load(object sender, EventArgs e)
		{
			var scoreId = Request.QueryString["id"].GetInt();
			if (!IsPostBack)
			{
				if(!User.Identity.Name.StartsWith("admin"))
				if (!_scoreRepository.TableNoTracking.Any(s => 
				s.ID == scoreId && s.Paper.EndExamTime <= DateTime.Now))
				{
					Response.Write("考试未结束");
					Response.End( );
                        return;
				}
                var score = _scoreRepository.GetById(scoreId);
                PaperName = _paperRepository.GetById(score.PaperID).PaperName;

            }
		}

		private void SetScore(int scoreId) {
			var score = _scoreRepository.GetById(scoreId);
			var paperInfos = score.Paper.PaperInfo.ToList();

			var userValues = _userAnswerRepository.TableNoTracking.Where(s=>s.ScoreID==scoreId && s.UserMark==0);

			var spList = _ctx.SingleProblem.AsNoTracking().Where(s => s.PaperID == score.PaperID).ToList();
			var mpList = _ctx.MultiProblem.AsNoTracking().Where(s => s.PaperID == score.PaperID).ToList();
			var jpList = _ctx.JudgeProblem.AsNoTracking().Where(s => s.PaperID == score.PaperID).ToList();
			 

			var sMark = paperInfos.FirstOrDefault(s => s.QuestionType == Data.Paper.GetQuestionType(Data.QuestionType.Single)).Mark;
			var mMark = paperInfos.FirstOrDefault(s => s.QuestionType == Data.Paper.GetQuestionType(Data.QuestionType.Multi)).Mark;
			var jMark = paperInfos.FirstOrDefault(s => s.QuestionType == Data.Paper.GetQuestionType(Data.QuestionType.Judge)).Mark;
		 
			foreach (var item in userValues)
			{
				var tempType = Data.Paper.GetQuestionTypeEnum(item.Type);
				switch (tempType)
				{
					case Data.QuestionType.Single:
						item.Mark = sMark;
						item.UserMark = spList.Any(s => s.ID == item.TitleID && s.Answer == item.UserAnswer1) ? item.Mark : 0;
						break;
					case Data.QuestionType.Multi:
						item.Mark = mMark;
						item.UserMark = mpList.Any(s => s.ID == item.TitleID && s.Answer == item.UserAnswer1) ? item.Mark : 0;
						break;
					case Data.QuestionType.Judge:
						item.Mark = jMark;
						item.UserMark = jpList.Any(s => s.ID == item.TitleID && s.Answer == (item.UserAnswer1 == "正确")) ? item.Mark : 0;
						break;
					case Data.QuestionType.Fill:
						break;
					case Data.QuestionType.Qustion:
						break;
					case Data.QuestionType.Other:
						break;
					default:
						break;
				}
			}

			_userAnswerRepository.Update(userValues);

		}
	}
}